package com.novoda.runbuddy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.IOptionsMenu;
import com.novoda.runbuddy.controller.ITargetController;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.controller.TargetControllerImpl;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;

public class Target extends Activity {

	private ITargetController	targetController;
	private IOptionsMenu			optionsMenu;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.target);
		optionsMenu = new OptionsMenuImpl(this);
		targetController = new TargetControllerImpl(this);
		
		targetController.createTargetView(this, (CheckBox) findViewById(R.id.checkbox_target),
				(TextView) findViewById(R.id.hours), (TextView) findViewById(R.id.minutes), (TextView) findViewById(R.id.seconds),
				(TextView) findViewById(R.id.milliseconds), (Button) findViewById(R.id.hours_up),
				(Button) findViewById(R.id.hours_down), (TextView) findViewById(R.id.hours_sep),
				(Button) findViewById(R.id.minutes_up), (Button) findViewById(R.id.minutes_down),
				(TextView) findViewById(R.id.minutes_sep), (Button) findViewById(R.id.secs_up),
				(Button) findViewById(R.id.secs_down), (TextView) findViewById(R.id.seconds_sep),
				(Button) findViewById(R.id.millisecs_up), (Button) findViewById(R.id.millisecs_down));
		;
	}

	public Context getContext() {
		return this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

	@Override
	protected void onResume() {
		RemoteStopClockServiceInterface.getInstance().setUpdateHandler(targetController.getUpdateHandler());
		targetController.onResume();
		super.onResume();
	}

	@Override
	protected void onPause() {
		RemoteStopClockServiceInterface.getInstance().clearUpdateHandler(targetController.getUpdateHandler());
		super.onPause();
	}

	public static void show(Activity activity) {
		// Only start new activity if we're currently on a different activity
		if (activity.getLocalClassName().compareTo("activity.Stopwatch") != 0) {
			Intent intent = new Intent(activity, TabsAdapter.class);
			Bundle extras = new Bundle();
			extras.putString("name0", "Time");
			extras.putString("tab0", "Stopwatch");
			extras.putString("name1", "Target");
			extras.putString("tab1", "Stopwatch");
			extras.putString("name2", "Routes");
			extras.putString("tab2", "RouteRunner");
			extras.putString("name3", "Location");
			extras.putString("tab3", "CurrentRunMap");
			extras.putInt("default", 0);
			intent.putExtras(extras);
			activity.startActivityForResult(intent, ActivityID.STOPCLOCK);
		}
	}
}
